{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.12 secs ==> Document-Term Matrix\n",
      "15.35 secs ==> TF-IDF transform\n",
      "4.07 secs ==> Document-Term Matrix\n",
      "14.91 secs ==> TF-IDF transform\n",
      "Train 25000 samples | Test 25000 samples\n",
      "Epoch 1/2 | Step 50/781 | train_loss: 0.6383 | train_acc: 0.7500 | lr: 0.0050\n",
      "Epoch 1/2 | Step 100/781 | train_loss: 0.6063 | train_acc: 0.7812 | lr: 0.0050\n",
      "Epoch 1/2 | Step 150/781 | train_loss: 0.5803 | train_acc: 0.6562 | lr: 0.0050\n",
      "Epoch 1/2 | Step 200/781 | train_loss: 0.4920 | train_acc: 0.8438 | lr: 0.0050\n",
      "Epoch 1/2 | Step 250/781 | train_loss: 0.4728 | train_acc: 0.8438 | lr: 0.0050\n",
      "Epoch 1/2 | Step 300/781 | train_loss: 0.4091 | train_acc: 0.8750 | lr: 0.0050\n",
      "Epoch 1/2 | Step 350/781 | train_loss: 0.4178 | train_acc: 0.8750 | lr: 0.0050\n",
      "Epoch 1/2 | Step 400/781 | train_loss: 0.4227 | train_acc: 0.8750 | lr: 0.0050\n",
      "Epoch 1/2 | Step 450/781 | train_loss: 0.3546 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 1/2 | Step 500/781 | train_loss: 0.3709 | train_acc: 0.8750 | lr: 0.0050\n",
      "Epoch 1/2 | Step 550/781 | train_loss: 0.3997 | train_acc: 0.7812 | lr: 0.0050\n",
      "Epoch 1/2 | Step 600/781 | train_loss: 0.3228 | train_acc: 0.9062 | lr: 0.0050\n",
      "Epoch 1/2 | Step 650/781 | train_loss: 0.3140 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 1/2 | Step 700/781 | train_loss: 0.3296 | train_acc: 0.9062 | lr: 0.0050\n",
      "Epoch 1/2 | Step 750/781 | train_loss: 0.3609 | train_acc: 0.8438 | lr: 0.0050\n",
      "Epoch 1/2 | train_loss: 0.1946 | train_acc: 1.0000 | test_loss: 0.3443 | test_acc: 0.8836 | lr: 0.0050\n",
      "Epoch 2/2 | Step 50/781 | train_loss: 0.3954 | train_acc: 0.9062 | lr: 0.0050\n",
      "Epoch 2/2 | Step 100/781 | train_loss: 0.2814 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 2/2 | Step 150/781 | train_loss: 0.3615 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 2/2 | Step 200/781 | train_loss: 0.2755 | train_acc: 0.9688 | lr: 0.0050\n",
      "Epoch 2/2 | Step 250/781 | train_loss: 0.2322 | train_acc: 0.9688 | lr: 0.0050\n",
      "Epoch 2/2 | Step 300/781 | train_loss: 0.2098 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 2/2 | Step 350/781 | train_loss: 0.3368 | train_acc: 0.8750 | lr: 0.0050\n",
      "Epoch 2/2 | Step 400/781 | train_loss: 0.1638 | train_acc: 1.0000 | lr: 0.0050\n",
      "Epoch 2/2 | Step 450/781 | train_loss: 0.1868 | train_acc: 1.0000 | lr: 0.0050\n",
      "Epoch 2/2 | Step 500/781 | train_loss: 0.2744 | train_acc: 0.9062 | lr: 0.0050\n",
      "Epoch 2/2 | Step 550/781 | train_loss: 0.1935 | train_acc: 0.9688 | lr: 0.0050\n",
      "Epoch 2/2 | Step 600/781 | train_loss: 0.2198 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 2/2 | Step 650/781 | train_loss: 0.2199 | train_acc: 0.9688 | lr: 0.0050\n",
      "Epoch 2/2 | Step 700/781 | train_loss: 0.2595 | train_acc: 0.9375 | lr: 0.0050\n",
      "Epoch 2/2 | Step 750/781 | train_loss: 0.2536 | train_acc: 0.8750 | lr: 0.0050\n",
      "Epoch 2/2 | train_loss: 0.2410 | train_acc: 0.8750 | test_loss: 0.2952 | test_acc: 0.8898 | lr: 0.0050\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       0.89      0.89      0.89     12500\n",
      "          1       0.89      0.89      0.89     12500\n",
      "\n",
      "avg / total       0.89      0.89      0.89     25000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import time\n",
    "\n",
    "from logistic_regression import LogisticRegression\n",
    "from sklearn.feature_extraction.text import TfidfTransformer\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "\n",
    "VOCAB_SIZE = 30000\n",
    "\n",
    "\n",
    "def transform(X, tfidf):\n",
    "    t0 = time.time()\n",
    "    count = np.zeros((len(X), VOCAB_SIZE))\n",
    "    for i, indices in enumerate(X):\n",
    "        for idx in indices:\n",
    "            count[i, idx] += 1\n",
    "    print(\"%.2f secs ==> Document-Term Matrix\"%(time.time()-t0))\n",
    "\n",
    "    t0 = time.time()\n",
    "    X = tfidf.fit_transform(count)\n",
    "    print(\"%.2f secs ==> TF-IDF transform\"%(time.time()-t0))\n",
    "    return X\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    (X_train, y_train), (X_test, y_test) = tf.keras.datasets.imdb.load_data(\n",
    "        num_words=VOCAB_SIZE)\n",
    "    \n",
    "    tfidf = TfidfTransformer()\n",
    "    X_train = transform(X_train, tfidf)\n",
    "    X_test = transform(X_test, tfidf)\n",
    "\n",
    "    model = LogisticRegression(VOCAB_SIZE, 2)\n",
    "    model.fit(X_train, y_train, n_epoch=2, batch_size=32, val_data=(X_test, y_test),\n",
    "              en_exp_decay=False)\n",
    "    \n",
    "    print(classification_report(y_test, model.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
